﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace TeacherHelp.FormList.Teacher
{
    public partial class SubjectsEditForm : BanFlashingForm
    {

        public SubjectsEditForm()
        {
            InitializeComponent();
            InitGrideView();
        }

        private void InitGrideView()
        {
            BLL.SubjectsManager subjectsManager = new BLL.SubjectsManager();
            this.SubjectsInfoGridView.DataSource = subjectsManager.GetSubejctAll();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(SubjectsNameTxt.Text.Trim())) return;
            BLL.SubjectsManager subjectsManager = new BLL.SubjectsManager();

            //添加
            Model.Subjects subjects = new Model.Subjects();
            subjects.Name = SubjectsNameTxt.Text.Trim();
            var result = subjectsManager.AddSubject(subjects);
            InitGrideView();
            MessageBox.Show(result ?? "添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

        }

        private void refershBtn_Click(object sender, EventArgs e)
        {
            InitGrideView();
        }


        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }

            var selectRows = SubjectsInfoGridView.SelectedRows;
            if (selectRows.Count <= 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var index = selectRows[0].Cells[nameof(Id)].Value.ToString();
            BLL.SubjectsManager subjectsManager = new BLL.SubjectsManager();
            var result = subjectsManager.RemoveSubject(index);
            if (string.IsNullOrEmpty(result))
            {
                InitGrideView();
            }
            MessageBox.Show(result ?? "添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

        }

        private void SubjectsInfoGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                Model.Subjects subjects = new Model.Subjects();
                subjects.Id = SubjectsInfoGridView.Rows[e.RowIndex].Cells[nameof(Id)].Value.ToInt();
                subjects.Name = SubjectsInfoGridView.Rows[e.RowIndex].Cells[nameof(SubjectsName)].Value.ToString();

                BLL.SubjectsManager subjectsManager = new BLL.SubjectsManager();
                var result = subjectsManager.EditSubject(subjects);
                if (string.IsNullOrEmpty(result))
                {
                    InitGrideView();
                }
                MessageBox.Show(result ?? "修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            }
        }
    }
}
